package com.hr.borrowingLog.service.impl;

import java.util.Date;
import java.util.List;

import com.hr.borrowing.domain.Borrowing;
import com.hr.borrowing.mapper.BorrowingMapper;
import com.hr.common.core.domain.model.LoginUser;
import com.hr.common.utils.DateUtils;
import com.hr.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hr.borrowingLog.mapper.BorrowingLogMapper;
import com.hr.borrowingLog.domain.BorrowingLog;
import com.hr.borrowingLog.service.IBorrowingLogService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

import static com.hr.common.utils.SecurityUtils.getLoginUser;

/**
 * 查询借支记录Service业务层处理
 * 
 * @author guan
 * @date 2024-07-20
 */
@Service
public class BorrowingLogServiceImpl implements IBorrowingLogService 
{
    @Resource
    private BorrowingLogMapper borrowingLogMapper;

    @Resource
    private BorrowingMapper borrowingMapper;


    /**
     * 查询查询借支记录
     * 
     * @param borrowingLogId 查询借支记录主键
     * @return 查询借支记录
     */
    @Override
    public BorrowingLog selectBorrowingLogByBorrowingLogId(Long borrowingLogId)
    {
        return borrowingLogMapper.selectBorrowingLogByBorrowingLogId(borrowingLogId);
    }

    /**
     * 查询查询借支记录列表
     * 
     * @param borrowingLog 查询借支记录
     * @return 查询借支记录
     */
    @Override
    public List<BorrowingLog> selectBorrowingLogList(BorrowingLog borrowingLog)
    {
        List<Date> times = borrowingLog.getTimes1();
        if (times != null && times.size() > 0) {
            borrowingLog.setStartTime(times.get(0));
            borrowingLog.setendTime(times.get(1));
        }
        return borrowingLogMapper.selectBorrowingLogList(borrowingLog);
    }

    /**
     * 新增查询借支记录
     * 
     * @param borrowingLog 查询借支记录
     * @return 结果
     */
    @Override
    public int insertBorrowingLog(BorrowingLog borrowingLog)
    {
        borrowingLog.setCreateTime(DateUtils.getNowDate());
        String userName = getLoginUser().getUser().getNickName();
        borrowingLog.setCreateBy(userName);
        return borrowingLogMapper.insertBorrowingLog(borrowingLog);
    }

    /**
     * 修改查询借支记录
     * 
     * @param borrowingLog 查询借支记录
     * @return 结果
     */
    @Override
    @Transactional
    public int updateBorrowingLog(BorrowingLog borrowingLog)
    {
        borrowingLog.setUpdateTime(DateUtils.getNowDate());

        if (borrowingLog.getBorrowingStatus() == 1) {
            Long userId = borrowingLog.getUserId();
            Borrowing borrowing = new Borrowing();
            Borrowing bw = borrowingMapper.selectBorrowingByUserId(userId);
            borrowing.setUserId(userId);
            borrowing.setBorrowedAmount(bw.getBorrowedAmount() + borrowingLog.getborrowingLogAmount());
            borrowing.setBorrowedNumber(bw.getBorrowedNumber() + 1);
            borrowingMapper.updateBorrowing(borrowing);
        }
        return borrowingLogMapper.updateBorrowingLog(borrowingLog);
    }

    /**
     * 批量删除查询借支记录
     * 
     * @param borrowingLogIds 需要删除的查询借支记录主键
     * @return 结果
     */
    @Override
    public int deleteBorrowingLogByBorrowingLogIds(Long[] borrowingLogIds)
    {
        return borrowingLogMapper.deleteBorrowingLogByBorrowingLogIds(borrowingLogIds);
    }

    /**
     * 删除查询借支记录信息
     * 
     * @param borrowingLogId 查询借支记录主键
     * @return 结果
     */
    @Override
    public int deleteBorrowingLogByBorrowingLogId(Long borrowingLogId)
    {
        return borrowingLogMapper.deleteBorrowingLogByBorrowingLogId(borrowingLogId);
    }

    /**
     * 还款额统计
     */
    @Override
    public void repayment() {

    }


    /**
     * 用户查询与自身相关借支信息
     * @param borrowingLog
     * @return
     */
    @Override
    public List<BorrowingLog> selectBorrowingLogAssociationList(BorrowingLog borrowingLog) {
        return borrowingLogMapper.selectBorrowingLogAssociationList(borrowingLog);
    }

    /**
     * 统计金额
     * @param borrowingLog
     * @return
     */
    @Override
    public String countAmount(BorrowingLog borrowingLog) {
        borrowingLog.setStartTime(borrowingLog.getTimes2().get(0));
        borrowingLog.setendTime(borrowingLog.getTimes2().get(1));
        return borrowingLogMapper.countAmount(borrowingLog);
    }
}
